package com.gpj.learn.rocketmq.delay;

import com.gpj.learn.rocketmq.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

/**
 * @className DelayTimeProducer
 * @description
 *   延迟消息的消息生产者者(需要指定消息延迟级别)
 * @author GPJ
 * @date 2021/4/26 21:39
 * @version 1.0
 **/
@Slf4j
public class DelayTimeProducer {
	public static void main(String[] args) {
		DefaultMQProducer producer = new DefaultMQProducer("DelayTimeMessageProducerGroup");
		producer.setNamesrvAddr(CommonConstants.ROCKETMQ_NAMESERVER_ADDR);
		try {
			producer.start();
			Message message = new Message(CommonConstants.DEFAULT_TOPIC_NAME,
					"TagA", "this is a delay message".getBytes(StandardCharsets.UTF_8));
			message.setDelayTimeLevel(3); // 设置延迟级别
			SendResult result = producer.send(message);
			log.info("消息发送结果{}", result);
		} catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
			e.printStackTrace();
		}finally {
			producer.shutdown();
		}

	}
}
